package dk.mirasola.systemtraining.biddingsession.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import dk.mirasola.systemtraining.biddingsession.client.util.BiddingSessionAsyncCallback;
import dk.mirasola.systemtraining.biddingsession.shared.service.SessionService;
import dk.mirasola.systemtraining.biddingsession.shared.service.SessionServiceAsync;
import dk.mirasola.systemtraining.biddingsession.shared.transfer.FinishBoardTO;
import dk.mirasola.systemtraining.biddingsession.shared.transfer.FinishInitDataTO;
import dk.mirasola.systemtraining.bridgewidgets.client.ui.BiddingSequenceTable;
import dk.mirasola.systemtraining.bridgewidgets.client.ui.DistributionView;

public class CompletedSessionPanel extends Composite {
    interface CompletedSessionPanelUiBinder extends UiBinder<Widget, CompletedSessionPanel> {}
    private static CompletedSessionPanelUiBinder uiBinder = GWT.create(CompletedSessionPanelUiBinder.class);
    private static SessionServiceAsync sessionService = GWT.create(SessionService.class);
 
    @UiField
    HTML header;
    @UiField
    BoardsPanel boardsPanel;
    @UiField
    BiddingSequenceTable biddingSequenceTable;
    @UiField
    DistributionView distributionView;
    @UiField
    FlowPanel commentsPanel;

    private String biddingSetId;

    public CompletedSessionPanel(String biddingSetId) {
        this.biddingSetId = biddingSetId;
        initWidget(uiBinder.createAndBindUi(this));
        initBoardsPanel();
        initData();
    }

    private void initData() {
        sessionService.getFinishInitData(biddingSetId, new BiddingSessionAsyncCallback<FinishInitDataTO>() {
            public void onSuccess(FinishInitDataTO data) {
                boardsPanel.setSessionBoards(data.getSessionBoards());
                boardsPanel.setSelectedBoardKey(data.getFirstBoard().getBoardKey());
                header.setHTML(data.getBiddingSetName());
                update(data.getFirstBoard());
            }
        });
    }


    private void initBoardsPanel() {
        boardsPanel.setChangeHandler(new ChangeHandler() {
            @Override
            public void onChange(ChangeEvent event) {
                String boardKey = boardsPanel.getSelectedBoardKey();
                sessionService.getFinishBoard(boardKey, new BiddingSessionAsyncCallback<FinishBoardTO>() {
                    @Override
                    public void onSuccess(FinishBoardTO board) {
                        update(board);
                    }
                });
            }
        });
    }

    private void update(FinishBoardTO board) {
        biddingSequenceTable.setBiddingSequence(board.getBiddingSequence());
        distributionView.setDistribution(board.getDistribution());
        commentsPanel.clear();
        if (board.getComments() != null) {
            for (String comment : board.getComments()) {
                commentsPanel.add(new HTML(comment));
            }
        }
    }
}
